<?php

declare(strict_types=1);

namespace App\Traits\Controller;



use App\Exception\InvalidRequestException;
use App\Request\FormRequest;
use Hyperf\Context\Context;
use Hyperf\HttpServer\Router\Dispatched;
use Hyperf\Collection\Arr;
use function Hyperf\Translation\__;

trait Controller
{
    /**
     * 获取排序
     * @param string $alias
     * @param array $order
     * @param string $pk
     * @param string $defaultOrders
     * @return array
     */
    protected function orderMap(string $alias='', array $order=[], string $pk='id', string $defaultOrders='desc'):array
    {
        if (empty($order)) {
            $order = ['field', 'order'];
        }
        $data = $this->request->all();
        $field = Arr::get($data, $order[0], $pk);
        $order = Arr::get($data, $order[1], $defaultOrders);
        $alias && $alias .= '.';

        return [
            $alias.$field,
            $order
        ];
    }

    /**
     * 获取每页条目数
     * @param int|string $default
     */
    protected function getLimit(int $default=30) :int|string
    {
        $limit = $this->request->input('limit', $default);
        if ($limit === 'all') {
            return 'all';
        }
        return (int)$limit;
    }

    /**
     * 获取表单
     * @param array $only
     * @param string|null $scene
     * @param bool $isValidate
     * @return array
     */
    protected function input(array $only=[], ?string $scene=null, bool $isValidate=true):array
    {
        if (!$this->requestClass) {
            /** @var FormRequest $request */
            $request = $this->container->get(FormRequest::class);
            return $only?$request->only($only):$request->getAll();
        }

        /** @var FormRequest $request */
        $request = $this->container->get($this->requestClass);
        if (true === $isValidate) {
            $request->scene($scene?:$request->getAction())->validateResolved();
        }

        return $only?
            $request->only($only):
            $request->getAll();
    }

    /**
     * 获取查询参数
     * @param array $only
     * @return array
     */
    protected function query(array $only=[])
    {
        return $this->input($only, null, false);
    }

    /**
     * 获取
     * @param $key
     * @return array|\ArrayAccess|mixed
     */
    protected function get($key, $isRequired=false)
    {
        if (strpos($key, '/')) {
            [$key, $type] = explode('/', $key);
        }
        $input = $this->input([$key], null, false);
        if (true === $isRequired && !Arr::has($input, $key)) {
            throw new InvalidRequestException(__('validation.required', ['attribute'=>$key]));
        }

        return Arr::get($input, $key);
    }

    /**
     * 获取文件
     * @param string $name
     * @param string|null $scene
     * @return \Hyperf\HttpMessage\Upload\UploadedFile|\Hyperf\HttpMessage\Upload\UploadedFile[]|null
     */
    protected function files(string $name, ?string $scene=null)
    {
        /** @var FormRequest $request */
        $request = $this->container->get($this->requestClass);
        $request->scene($scene?:$request->getAttribute(Dispatched::class)->handler->callback[1])->validateResolved();

        return $request->file($name);
    }

    /**
     * 权限判断
     * @param bool $isPower
     */
    protected function power(bool $isPower=true)
    {
        $adminInfo = Context::get('adminInfo');
        if ($adminInfo['is_admin'] != 1 && true === $isPower) {
            throw new InvalidRequestException(__('reponse.forbidden'));
        }
    }
}
